home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / reload1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  69.0 KB  |  2,096 lines

  1. /* Reload pseudo regs into hard regs for insns that require hard regs.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "flags.h"
  25. #include "regs.h"
  26. #include "hard-reg-set.h"
  27. #include "reload.h"
  28. #include "insn-config.h"
  29. #include "basic-block.h"
  30. #include <stdio.h>
  31. #include "alloca.h"
  32.  
  33. #define min(A,B) ((A) < (B) ? (A) : (B))
  34.  
  35. /* This file contains the reload pass of the compiler, which is
  36.    run after register allocation has been done.  It checks that
  37.    each insn is valid (operands required to be in registers really
  38.    are in registers of the proper class) and fixes up invalid ones
  39.    by copying values temporarily into registers for the insns
  40.    that need them.
  41.  
  42.    The results of register allocation are described by the vector
  43.    reg_renumber; the insns still contain pseudo regs, but reg_renumber
  44.    can be used to find which hard reg, if any, a pseudo reg is in.
  45.  
  46.    The technique we always use is to free up a few hard regs that are
  47.    called ``reload regs'', and for each place where a pseudo reg
  48.    must be in a hard reg, copy it temporarily into one of the reload regs.
  49.  
  50.    All the pseudos that were formerly allocated to the hard regs that
  51.    are now in use as reload regs must be ``spilled''.  This means
  52.    that they go to other hard regs, or to stack slots if no other
  53.    available hard regs can be found.  Spilling can invalidate more
  54.    insns, requiring additional need for reloads, so we must keep checking
  55.    until the process stabilizes.
  56.  
  57.    For machines with different classes of registers, we must keep track
  58.    of the register class needed for each reload, and make sure that
  59.    we allocate enough reload registers of each class.
  60.  
  61.    The file reload.c contains the code that checks one insn for
  62.    validity and reports the reloads that it needs.  This file
  63.    is in charge of scanning the entire rtl code, accumulating the
  64.    reload needs, spilling, assigning reload registers to use for
  65.    fixing up each insn, and generating the new insns to copy values
  66.    into the reload registers.  */
  67.  
  68. /* During reload_as_needed, element N contains a REG rtx for the hard reg
  69.    into which pseudo reg N has been reloaded (perhaps for a previous insn). */
  70. static rtx *reg_last_reload_reg;
  71.  
  72. /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
  73.    for an output reload that stores into reg N.  */
  74. static char *reg_has_output_reload;
  75.  
  76. /* Element N is the constant value to which pseudo reg N is equivalent,
  77.    or zero if pseudo reg N is not equivalent to a constant.
  78.    find_reloads looks at this in order to replace pseudo reg N
  79.    with the constant it stands for.  */
  80. rtx *reg_equiv_constant;
  81.  
  82. /* Element N is the address of stack slot to which pseudo reg N is equivalent.
  83.    This is used when the address is not valid as a memory address
  84.    (because its displacement is too big for the machine.)  */
  85. rtx *reg_equiv_address;
  86.  
  87. /* Element N is the memory slot to which pseudo reg N is equivalent,
  88.    or zero if pseudo reg N is not equivalent to a memory slot.  */
  89. static rtx *reg_equiv_mem;
  90.  
  91. /* Element N is the insn that initialized reg N from its equivalent
  92.    constant or memory slot.  */
  93. static rtx *reg_equiv_init;
  94.  
  95. /* During reload_as_needed, element N contains the last pseudo regno
  96.    reloaded into the Nth reload register.  This vector is in parallel
  97.    with spill_regs.  */
  98. static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
  99.  
  100. /* Number of spill-regs so far; number of valid elements of spill_regs.  */
  101. static int n_spills;
  102.  
  103. /* In parallel with spill_regs, contains REG rtx's for those regs.
  104.    Holds the last rtx used for any given reg, or 0 if it has never
  105.    been used for spilling yet.  This rtx is reused, provided it has
  106.    the proper mode.  */
  107. static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
  108.  
  109. /* In parallel with spill_regs, contains nonzero for a spill reg
  110.    that was stored after the last time it was used.
  111.    The precise value is the insn generated to do the store.  */
  112. static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
  113.  
  114. /* This table is the inverse mapping of spill_regs:
  115.    indexed by hard reg number,
  116.    it contains the position of that reg in spill_regs,
  117.    or -1 for something that is not in spill_regs.  */
  118. static short spill_reg_order[FIRST_PSEUDO_REGISTER];
  119.  
  120. /* This table contains 1 for a register that may not be used
  121.    for retrying global allocation, or -1 for a register that may be used.
  122.    The registers that may not be used include all spill registers
  123.    and the frame pointer (if we are using one).  */
  124. static short forbidden_regs[FIRST_PSEUDO_REGISTER];
  125.  
  126. /* Describes order of use of registers for reloading
  127.    of spilled pseudo-registers.  `spills' is the number of
  128.    elements that are actually valid; new ones are added at the end.  */
  129. static char spill_regs[FIRST_PSEUDO_REGISTER];
  130.  
  131. /* Describes order of preference for putting regs into spill_regs.
  132.    Contains the numbers of all the hard regs, in order most preferred first.
  133.    This order is different for each function.
  134.    It is set up by order_regs_for_reload.
  135.    Empty elements at the end contain -1.  */
  136. static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
  137.  
  138. /* 1 for a hard register that appears explicitly in the rtl
  139.    (for example, function value registers, special registers
  140.    used by insns, structure value pointer registers).  */
  141. static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
  142.  
  143. /* Nonzero if spilling (REG n) does not require reloading it into
  144.    a register in order to do (MEM (REG n)).  */
  145.  
  146. static char spill_indirect_ok;
  147.  
  148. /* Indexed by basic block number, nonzero if there is any need
  149.    for a spill register in that basic block.
  150.    The pointer is 0 if we did stupid allocation and don't know
  151.    the structure of basic blocks.  */
  152.  
  153. char *basic_block_needs;
  154.  
  155. /* First uid used by insns created by reload in this function.
  156.    Used in find_equiv_reg.  */
  157. int reload_first_uid;
  158.  
  159. void mark_home_live ();
  160. static void reload_as_needed ();
  161. static rtx alter_frame_pointer_addresses ();
  162. static void alter_reg ();
  163. static int new_spill_reg();
  164. static int spill_hard_reg ();
  165. static void choose_reload_targets ();
  166. static void forget_old_reloads ();
  167. static void order_regs_for_reload ();
  168. static void eliminate_frame_pointer ();
  169.  
  170. extern void remove_death ();
  171. extern rtx adj_offsetable_operand ();
  172.  
  173. /* Main entry point for the reload pass, and only entry point
  174.    in this file.
  175.  
  176.    FIRST is the first insn of the function being compiled.
  177.  
  178.    GLOBAL nonzero means we were called from global_alloc
  179.    and should attempt to reallocate any pseudoregs that we
  180.    displace from hard regs we will use for reloads.
  181.    If GLOBAL is zero, we do not have enough information to do that,
  182.    so any pseudo reg that is spilled must go to the stack.
  183.  
  184.    DUMPFILE is the global-reg debugging dump file stream, or 0.
  185.    If it is nonzero, messages are written to it to describe
  186.    which registers are seized as reload regs, which pseudo regs
  187.    are spilled from them, and where the pseudo regs are reallocated to.  */
  188.  
  189. void
  190. reload (first, global, dumpfile)
  191.      rtx first;
  192.      int global;
  193.      FILE *dumpfile;
  194. {
  195.   register int class;
  196.   register int i;
  197.   register rtx insn;
  198.  
  199.   int something_changed;
  200.   int something_needs_reloads;
  201.   int new_basic_block_needs;
  202.  
  203.   /* The basic block number currently being processed for INSN.  */
  204.   int this_block;
  205.  
  206.   /* Enable find_equiv_reg to distinguish insns made by reload.  */
  207.   reload_first_uid = get_max_uid ();
  208.  
  209.   basic_block_needs = 0;
  210.  
  211.   /* Remember which hard regs appear explicitly
  212.      before we merge into `regs_ever_live' the ones in which
  213.      pseudo regs have been allocated.  */
  214.   bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
  215.  
  216.   /* Compute which hard registers are now in use
  217.      as homes for pseudo registers.
  218.      This is done here rather than (eg) in global_alloc
  219.      because this point is reached even if not optimizing.  */
  220.  
  221.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  222.     mark_home_live (i);
  223.  
  224.   /* Find all the pseudo registers that didn't get hard regs
  225.      but do have known equivalent constants or memory slots.
  226.      These include parameters (known equivalent to parameter slots)
  227.      and cse'd or loop-moved constant memory addresses.
  228.  
  229.      Record constant equivalents in reg_equiv_constant
  230.      so they will be substituted by find_reloads.
  231.      Record memory equivalents in reg_mem_equiv so they can
  232.      be substituted eventually by altering the REG-rtx's.  */
  233.  
  234.   reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
  235.   bzero (reg_equiv_constant, max_regno * sizeof (rtx));
  236.   reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
  237.   bzero (reg_equiv_mem, max_regno * sizeof (rtx));
  238.   reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
  239.   bzero (reg_equiv_init, max_regno * sizeof (rtx));
  240.   reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
  241.   bzero (reg_equiv_address, max_regno * sizeof (rtx));
  242.  
  243.   for (insn = first; insn; insn = NEXT_INSN (insn))
  244.     if (GET_CODE (insn) == INSN
  245.     && GET_CODE (PATTERN (insn)) == SET
  246.     && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  247.       {
  248.     rtx note = find_reg_note (insn, REG_EQUIV, 0);
  249.     if (note)
  250.       {
  251.         rtx x = XEXP (note, 0);
  252.         i = REGNO (SET_DEST (PATTERN (insn)));
  253.         if (GET_CODE (x) == MEM)
  254.           reg_equiv_mem[i] = x;
  255.         else if (immediate_operand (x))
  256.           reg_equiv_constant[i] = x;
  257.         else
  258.           continue;
  259.  
  260.         reg_equiv_init[i] = insn;
  261.       }
  262.       }
  263.  
  264.   /* Does this function require a frame pointer?  */
  265.  
  266.   frame_pointer_needed
  267.     |= (! global || FRAME_POINTER_REQUIRED);
  268.  
  269.   if (! frame_pointer_needed)
  270.     frame_pointer_needed
  271.       = check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem);
  272.  
  273.   /* Alter each pseudo-reg rtx to contain its hard reg number.
  274.      Delete initializations of pseudos that don't have hard regs
  275.      and do have equivalents.
  276.      Assign stack slots to the pseudos that lack hard regs or equivalents.  */
  277.  
  278.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  279.     alter_reg (i);
  280.  
  281. #ifndef REGISTER_CONSTRAINTS
  282.   /* If all the pseudo regs have hard regs,
  283.      except for those that are never referenced,
  284.      we know that no reloads are needed.  */
  285.   /* But that is not true if there are register constraints, since
  286.      in that case some pseudos might be in the wrong kind of hard reg.  */
  287.  
  288.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  289.     if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
  290.       break;
  291.  
  292.   if (i == max_regno && frame_pointer_needed)
  293.     return;
  294. #endif
  295.  
  296.   /* Compute the order of preference for hard registers to spill.
  297.      Store them by decreasing preference in potential_reload_regs.  */
  298.  
  299.   order_regs_for_reload ();
  300.  
  301.   /* So far, no hard regs have been spilled.  */
  302.   n_spills = 0;
  303.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  304.     {
  305.       spill_reg_order[i] = -1;
  306.       forbidden_regs[i] = -1;
  307.     }
  308.  
  309.   if (frame_pointer_needed)
  310.     {
  311.       forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  312.       spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  313.     }
  314.  
  315.   if (global)
  316.     {
  317.       basic_block_needs = (char *)alloca (n_basic_blocks);
  318.       bzero (basic_block_needs, n_basic_blocks);
  319.     }
  320.  
  321.   /* This loop scans the entire function each go-round
  322.      and repeats until one repetition spills no additional hard regs.  */
  323.  
  324.   /* This flag is set when a psuedo reg is spilled,
  325.      to require another pass.  Note that getting an additional reload
  326.      reg does not necessarily imply any pseudo reg was spilled;
  327.      sometimes we find a reload reg that no pseudo reg was allocated in.  */
  328.   something_changed = 1;
  329.   /* This flag is set if there are any insns that require reloading.  */
  330.   something_needs_reloads = 0;
  331.   while (something_changed)
  332.     {
  333.       /* For each class, number of reload regs needed in that class.
  334.      This is the maximum over all insns of the needs in that class
  335.      of the individual insn.  */
  336.       int max_needs[N_REG_CLASSES];
  337.       /* For each class, size of group of consecutive regs
  338.      that is needed for the reloads of this class.  */
  339.       int group_size[N_REG_CLASSES];
  340.       /* For each class, max number of consecutive groups needed.
  341.      (Each group contains max_needs_size[CLASS] consecutive registers.)  */
  342.       int max_groups[N_REG_CLASSES];
  343.       /* For each class, max number needed of regs that don't belong
  344.      to any of the groups.  */
  345.       int max_nongroups[N_REG_CLASSES];
  346.       /* For each class, the machine mode which requires consecutive
  347.      groups of regs of that class.
  348.      If two different modes ever require groups of one class,
  349.      they must be tieable and the same number of regs;
  350.      otherwise we can't handle the complexity.  */
  351.       enum machine_mode group_mode[N_REG_CLASSES];
  352.       /* For each register, 1 if it was counted against the need for
  353.      groups.  0 means it can count against max_nongroup instead.  */
  354.       char counted_for_groups[FIRST_PSEUDO_REGISTER];
  355.       /* For each register, 1 if it was counted against the need for
  356.      non-groups.  0 means it can become part of a new group.  */
  357.       char counted_for_nongroups[FIRST_PSEUDO_REGISTER];
  358.  
  359.       something_changed = 0;
  360.       bzero (max_needs, sizeof max_needs);
  361.       bzero (max_groups, sizeof max_groups);
  362.       bzero (max_nongroups, sizeof max_nongroups);
  363.       bzero (group_size, sizeof group_size);
  364.       for (i = 0; i < N_REG_CLASSES; i++)
  365.     group_mode[i] = VOIDmode;
  366.  
  367.       /* Keep track of which basic blocks are needing the reloads.  */
  368.       this_block = 0;
  369.  
  370.       /* Remember whether any element of basic_block_needs
  371.      changes from 0 to 1 in this pass.  */
  372.       new_basic_block_needs = 0;
  373.  
  374.       /* Compute the most additional registers needed by any instruction.
  375.      Collect information separately for each class of regs.  */
  376.  
  377.       for (insn = first; insn; insn = NEXT_INSN (insn))
  378.     {
  379.       if (global && this_block + 1 < n_basic_blocks
  380.           && insn == basic_block_head[this_block+1])
  381.         ++this_block;
  382.  
  383.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  384.           || GET_CODE (insn) == CALL_INSN)
  385.         {
  386.           int insn_needs[N_REG_CLASSES];
  387.           int insn_groups[N_REG_CLASSES];
  388.           int insn_total_groups = 0;
  389.  
  390.           for (i = 0; i < N_REG_CLASSES; i++)
  391.         insn_needs[i] = 0, insn_groups[i] = 0;
  392.  
  393. #if 0
  394.           /* Optimization: a bit-field instruction whose field
  395.          happens to be a byte or halfword in memory
  396.          can be changed to a move instruction.  */
  397.  
  398.           if (GET_CODE (PATTERN (insn)) == SET)
  399.         {
  400.           rtx dest = SET_DEST (PATTERN (insn));
  401.           rtx src = SET_SRC (PATTERN (insn));
  402.  
  403.           if (GET_CODE (dest) == ZERO_EXTRACT
  404.               || GET_CODE (dest) == SIGN_EXTRACT)
  405.             optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  406.           if (GET_CODE (src) == ZERO_EXTRACT
  407.               || GET_CODE (src) == SIGN_EXTRACT)
  408.             optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  409.         }
  410. #endif
  411.  
  412.           /* Analyze the instruction.  */
  413.  
  414.           find_reloads (insn, 0, spill_indirect_ok, global, spill_reg_order);
  415.  
  416.           if (n_reloads == 0)
  417.         continue;
  418.  
  419.           something_needs_reloads = 1;
  420.  
  421.           /* Count each reload once in every class
  422.          containing the reload's own class.  */
  423.  
  424.           for (i = 0; i < n_reloads; i++)
  425.         {
  426.           register enum reg_class *p;
  427.           int size;
  428.           enum machine_mode mode;
  429.  
  430.           /* Don't count the dummy reloads, for which one of the
  431.              regs mentioned in the insn can be used for reloading.
  432.              Don't count optional reloads.
  433.              Don't count reloads that got combined with others.  */
  434.           if (reload_reg_rtx[i] != 0
  435.               || reload_optional[i] != 0
  436.               || (reload_out[i] == 0 && reload_in[i] == 0))
  437.             continue;
  438.  
  439.           mode = reload_inmode[i];
  440.           if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
  441.             mode = reload_outmode[i];
  442.           size = CLASS_MAX_NREGS (reload_reg_class[i], mode);
  443.           if (size > 1)
  444.             {
  445.               /* Count number of groups needed separately from
  446.              number of individual regs needed.  */
  447.               insn_groups[(int) reload_reg_class[i]]++;
  448.               p = reg_class_superclasses[(int) reload_reg_class[i]];
  449.               while (*p != LIM_REG_CLASSES)
  450.             insn_groups[(int) *p++]++;
  451.               insn_total_groups++;
  452.  
  453.               /* If a group of consecutive regs are needed,
  454.              record which machine mode needs them.
  455.              Crash if two dissimilar machine modes both need
  456.              groups of consecutive regs of the same class.  */
  457.  
  458.               if (group_mode[(int) reload_reg_class[i]] != VOIDmode
  459.               &&
  460.               (!MODES_TIEABLE_P (group_mode[(int) reload_reg_class[i]], mode)
  461.                ||
  462.                group_size[(int) reload_reg_class[i]] != size))
  463.             abort ();
  464.  
  465.               /* Record size and mode of a group of this class.  */
  466.               group_size[(int) reload_reg_class[i]] = size;
  467.               group_mode[(int) reload_reg_class[i]] = mode;
  468.             }
  469.           else if (size == 1)
  470.             {
  471.               insn_needs[(int) reload_reg_class[i]] += 1;
  472.               p = reg_class_superclasses[(int) reload_reg_class[i]];
  473.               while (*p != LIM_REG_CLASSES)
  474.             insn_needs[(int) *p++] += 1;
  475.             }
  476.           else
  477.             abort ();
  478.  
  479.           if (global)
  480.             {
  481.               if (! basic_block_needs[this_block])
  482.             new_basic_block_needs = 1;
  483.               basic_block_needs[this_block] = 1;
  484.             }
  485.         }
  486.  
  487.           /* Remember for later shortcuts which insns had any reloads.  */
  488.  
  489.           PUT_MODE (insn, n_reloads ? QImode : VOIDmode);
  490.  
  491.           /* For each class, collect maximum need of any insn */
  492.  
  493.           for (i = 0; i < N_REG_CLASSES; i++)
  494.         {
  495.           if (max_needs[i] < insn_needs[i])
  496.             max_needs[i] = insn_needs[i];
  497.           if (max_groups[i] < insn_groups[i])
  498.             max_groups[i] = insn_groups[i];
  499.           if (insn_total_groups > 0)
  500.             if (max_nongroups[i] < insn_needs[i])
  501.               max_nongroups[i] = insn_needs[i];
  502.         }
  503.         }
  504.       /* Note that there is a continue statement above.  */
  505.     }
  506.  
  507.       /* Now deduct from the needs for the registers already
  508.      available (already spilled).  */
  509.  
  510.       bzero (counted_for_groups, sizeof counted_for_groups);
  511.       bzero (counted_for_nongroups, sizeof counted_for_nongroups);
  512.  
  513.       /* Find all consecutive groups of spilled registers
  514.      and mark each group off against the need for such groups.  */
  515.  
  516.       for (i = 0; i < N_REG_CLASSES; i++)
  517.     if (group_size[i] > 1)
  518.       {
  519.         char regmask[FIRST_PSEUDO_REGISTER];
  520.         int j;
  521.  
  522.         bzero (regmask, sizeof regmask);
  523.         /* Make a mask of all the regs that are spill regs in class I.  */
  524.         for (j = 0; j < n_spills; j++)
  525.           if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
  526.           && !counted_for_groups[spill_regs[i]])
  527.         regmask[spill_regs[j]] = 1;
  528.         /* Find each consecutive group of them.  */
  529.         for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
  530.           if (regmask[j] && j + group_size[i] <= FIRST_PSEUDO_REGISTER
  531.           /* Next line in case group-mode for this class
  532.              demands an even-odd pair.  */
  533.           && HARD_REGNO_MODE_OK (j, group_mode[i]))
  534.         {
  535.           int k;
  536.           for (k = 1; k < group_size[i]; k++)
  537.             if (! regmask[j + k])
  538.               break;
  539.           if (k == group_size[i])
  540.             {
  541.               /* We found a group.  Mark it off against this class's
  542.              need for groups, and against each superclass too.  */
  543.               register enum reg_class *p;
  544.               max_groups[i]--;
  545.               p = reg_class_superclasses[i];
  546.               while (*p != LIM_REG_CLASSES)
  547.             max_groups[(int) *p++]--;
  548.               /* Don't count these registers again.  */ 
  549.               counted_for_groups[j] = 1;
  550.               for (k = 1; k < group_size[i]; k++)
  551.             counted_for_groups[j + k] = 1;
  552.             }
  553.           j += k;
  554.         }
  555.       }
  556.  
  557.       /* Now count all remaining spill regs against the individual need.
  558.      Those that weren't counted_for_groups in groups can also count against
  559.      the not-in-group need.  */
  560.  
  561.       for (i = 0; i < n_spills; i++)
  562.     {
  563.       register enum reg_class *p;
  564.       class = (int) REGNO_REG_CLASS (spill_regs[i]);
  565.  
  566.       max_needs[class]--;
  567.       p = reg_class_superclasses[class];
  568.       while (*p != LIM_REG_CLASSES)
  569.         max_needs[(int) *p++]--;
  570.  
  571.       if (! counted_for_groups[spill_regs[i]])
  572.         {
  573.           if (max_nongroups[class] > 0)
  574.         counted_for_nongroups[spill_regs[i]] = 1;
  575.           max_nongroups[class]--;
  576.           p = reg_class_superclasses[class];
  577.           while (*p != LIM_REG_CLASSES)
  578.         {
  579.           if (max_nongroups[(int) *p] > 0)
  580.             counted_for_nongroups[spill_regs[i]] = 1;
  581.           max_nongroups[(int) *p++]--;
  582.         }
  583.         }
  584.     }
  585.  
  586.       /* If all needs are met, we win.  */
  587.  
  588.       for (i = 0; i < N_REG_CLASSES; i++)
  589.     if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
  590.       break;
  591.       if (i == N_REG_CLASSES && !new_basic_block_needs)
  592.     break;
  593.  
  594.       /* Not all needs are met; must spill more hard regs.  */
  595.  
  596.       /* If any element of basic_block_needs changed from 0 to 1,
  597.      re-spill all the regs already spilled.  This may spill
  598.      additional pseudos that didn't spill before.  */
  599.  
  600.       if (new_basic_block_needs)
  601.     for (i = 0; i < n_spills; i++)
  602.       something_changed
  603.         |= spill_hard_reg (spill_regs[i], global, dumpfile);
  604.  
  605.       /* Now find more reload regs to satisfy the remaining need
  606.      Do it by ascending class number, since otherwise a reg
  607.      might be spilled for a big class and might fail to count
  608.      for a smaller class even though it belongs to that class.
  609.  
  610.      Count spilled regs in `spills', and add entries to
  611.      `spill_regs' and `spill_reg_order'.  */
  612.  
  613.       for (class = 0; class < N_REG_CLASSES; class++)
  614.     {
  615.       /* First get the groups of registers.
  616.          If we got single registers first, we might fragment
  617.          possible groups.  */
  618.       while (max_groups[class] > 0)
  619.         {
  620.           /* Groups of size 2 (the only groups used on most machines)
  621.          are treated specially.  */
  622.           if (group_size[class] == 2)
  623.         {
  624.           /* First, look for a register that will complete a group.  */
  625.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  626.             {
  627.               int j = potential_reload_regs[i];
  628.               if (j >= 0
  629.               &&
  630.               ((j > 0 && spill_reg_order[j - 1] >= 0
  631.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  632.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j - 1)
  633.                 && HARD_REGNO_MODE_OK (j - 1, group_mode[class])
  634.                 && ! counted_for_nongroups[j - 1])
  635.                ||
  636.                (j < FIRST_PSEUDO_REGISTER - 1
  637.                 && spill_reg_order[j + 1] >= 0
  638.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  639.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  640.                 && HARD_REGNO_MODE_OK (j, group_mode[class])
  641.                 && ! counted_for_nongroups[j + 1])))
  642.             {
  643.               register enum reg_class *p;
  644.  
  645.               /* We have found one that will complete a group,
  646.                  so count off one group as provided.  */
  647.               max_groups[class]--;
  648.               p = reg_class_superclasses[class];
  649.               while (*p != LIM_REG_CLASSES)
  650.                 max_groups[(int) *p++]--;
  651.               break;
  652.             }
  653.             }
  654.           /* We can't complete a group, so start one.  */
  655.           if (i == FIRST_PSEUDO_REGISTER)
  656.             for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  657.               {
  658.             int j = potential_reload_regs[i];
  659.             if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
  660.                 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
  661.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  662.                 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  663.                 && HARD_REGNO_MODE_OK (j, group_mode[class])
  664.                 && ! counted_for_nongroups[j + 1])
  665.               break;
  666.               }
  667.  
  668.           /* I should be the index in potential_reload_regs
  669.              of the new reload reg we have found.  */
  670.  
  671.           something_changed
  672.             |= new_spill_reg (i, class, max_needs, 0,
  673.                       0, global, dumpfile);
  674.         }
  675.           else
  676.         {
  677.           /* For groups of more than 2 registers,
  678.              look for a sufficient sequence of unspilled registers,
  679.              and spill them all at once.  */
  680.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  681.             {
  682.               int j = potential_reload_regs[i];
  683.               int k;
  684.               if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
  685.               && HARD_REGNO_MODE_OK (j, group_mode[class]))
  686.             {
  687.               /* Check each reg in the sequence.  */
  688.               for (k = 0; k < group_size[class]; k++)
  689.                 if (! (spill_reg_order[j + k] < 0
  690.                    && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
  691.                   break;
  692.               /* We got a full sequence, so spill them all.  */
  693.               if (k == group_size[class])
  694.                 {
  695.                   register enum reg_class *p;
  696.                   for (k = 0; k < group_size[class]; k++)
  697.                 {
  698.                   int idx;
  699.                   for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
  700.                     if (potential_reload_regs[idx] == k)
  701.                       break;
  702.                   something_changed
  703.                     |= new_spill_reg (idx, class, max_needs, 0,
  704.                               0, global, dumpfile);
  705.                 }
  706.  
  707.                   /* We have found one that will complete a group,
  708.                  so count off one group as provided.  */
  709.                   max_groups[class]--;
  710.                   p = reg_class_superclasses[class];
  711.                   while (*p != LIM_REG_CLASSES)
  712.                 max_groups[(int) *p++]--;
  713.  
  714.                   break;
  715.                 }
  716.             }
  717.             }
  718.         }
  719.         }
  720.  
  721.       /* Now similarly satisfy all need for single registers.  */
  722.  
  723.       while (max_needs[class] > 0 || max_nongroups[class] > 0)
  724.         {
  725.           /* Consider the potential reload regs that aren't
  726.          yet in use as reload regs, in order of preference.
  727.          Find the most preferred one that's in this class.  */
  728.  
  729.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  730.         if (potential_reload_regs[i] >= 0
  731.             && TEST_HARD_REG_BIT (reg_class_contents[class],
  732.                       potential_reload_regs[i]))
  733.           break;
  734.  
  735.           /* I should be the index in potential_reload_regs
  736.          of the new reload reg we have found.  */
  737.  
  738.           something_changed
  739.         |= new_spill_reg (i, class, max_needs, max_nongroups,
  740.                   counted_for_nongroups, global, dumpfile);
  741.         }
  742.     }
  743.     }
  744.  
  745.   /* Now we know for certain whether we have a frame pointer.
  746.      If not, correct all references to go through the stack pointer.
  747.      This must be done before reloading, since reloading could generate
  748.      insns where sp+const cannot validly replace the frame pointer.
  749.      *This will lose if an insn might need more spill regs after
  750.      frame pointer elimination than it needed before.*  */
  751.  
  752.   if (! frame_pointer_needed)
  753.     eliminate_frame_pointer (first);
  754.  
  755.   /* Use the reload registers where necessary
  756.      by generating move instructions to move the must-be-register
  757.      values into or out of the reload registers.  */
  758.  
  759.   if (something_needs_reloads)
  760.     reload_as_needed (first, global);
  761.  
  762.   /* Now eliminate all pseudo regs by modifying them into
  763.      their equivalent memory references.
  764.      The REG-rtx's for the pseudos are modified in place,
  765.      so all insns that used to refer to them now refer to memory.
  766.  
  767.      For a reg that has a reg_equiv_address, all those insns
  768.      were changed by reloading so that no insns refer to it any longer;
  769.      but the DECL_RTL of a variable decl may refer to it,
  770.      and if so this causes the debugging info to mention the variable.  */
  771.  
  772.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  773.     {
  774.       rtx addr = 0;
  775.       if (reg_equiv_mem[i])
  776.     addr = XEXP (reg_equiv_mem[i], 0);
  777.       if (reg_equiv_address[i])
  778.     addr = reg_equiv_address[i];
  779.       if (addr)
  780.     {
  781.       if (reg_renumber[i] < 0)
  782.         {
  783.           rtx reg = regno_reg_rtx[i];
  784.           XEXP (reg, 0) = addr;
  785.           PUT_CODE (reg, MEM);
  786.         }
  787.       else if (reg_equiv_mem[i])
  788.         {
  789.           if (! frame_pointer_needed)
  790.         FIX_FRAME_POINTER_ADDRESS (addr, 0);
  791.           XEXP (reg_equiv_mem[i], 0) = addr;
  792.         }
  793.     }
  794.     }
  795. }
  796.  
  797. /* Add a new register REGNO to the tables of available spill-registers
  798.     (as well as spilling all pseudos allocated to the register).
  799.    CLASS is the regclass whose need is being satisfied.
  800.    MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
  801.     so that this register can count off against them.
  802.     MAX_NONGROUPS is 0 if this register is part of a group.
  803.    COUNTED_FOR_NONGROUPS is passed in so that we can indicate
  804.     whether this reg was used to count against MAX_NONGROUPS.
  805.    GLOBAL and DUMPFILE are the same as the args that `reload' got.  */
  806.  
  807. static int
  808. new_spill_reg (regno, class, max_needs, max_nongroups,
  809.            counted_for_nongroups, global, dumpfile)
  810.      int regno;
  811.      int class;
  812.      int *max_needs;
  813.      int *max_nongroups;
  814.      int *counted_for_nongroups;
  815.      int global;
  816.      FILE *dumpfile;
  817. {
  818.   register enum reg_class *p;
  819.   int val;
  820.  
  821.   if (regno >= FIRST_PSEUDO_REGISTER)
  822.     abort ();    /* Caller failed to find any register.  */
  823.  
  824.   /* Make potential_reload_regs[REGNO] an additional reload reg.  */
  825.  
  826.   spill_regs[n_spills] = potential_reload_regs[regno];
  827.   spill_reg_order[potential_reload_regs[regno]] = n_spills;
  828.   forbidden_regs[potential_reload_regs[regno]] = 1;
  829.   potential_reload_regs[regno] = -1;
  830.   if (dumpfile)
  831.     fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
  832.  
  833.   /* Clear off the needs we just satisfied.  */
  834.  
  835.   max_needs[class]--;
  836.   p = reg_class_superclasses[class];
  837.   while (*p != LIM_REG_CLASSES)
  838.     max_needs[(int) *p++]--;
  839.  
  840.   if (max_nongroups && max_nongroups[class] > 0)
  841.     {
  842.       counted_for_nongroups[regno] = 1;
  843.       max_nongroups[class]--;
  844.       p = reg_class_superclasses[class];
  845.       while (*p != LIM_REG_CLASSES)
  846.     max_nongroups[(int) *p++]--;
  847.     }
  848.  
  849.   /* Spill every pseudo reg that was allocated to this reg
  850.      or to something that overlaps this reg.  */
  851.  
  852.   val = spill_hard_reg (spill_regs[n_spills], global, dumpfile);
  853.  
  854.   regs_ever_live[spill_regs[n_spills]] = 1;
  855.   n_spills++;
  856.  
  857.   return val;
  858. }
  859.  
  860. /* Scan all insns, computing the stack depth, and convert all
  861.    frame-pointer-relative references to stack-pointer-relative references.  */
  862.  
  863. static void
  864. eliminate_frame_pointer (first)
  865.      rtx first;
  866. {
  867.   int depth = 0;
  868.   int max_uid = get_max_uid ();
  869.   int *label_depth = (int *) alloca ((max_uid + 1) * sizeof (int));
  870.   int i;
  871.   rtx insn;
  872.  
  873.   for (i = 0; i <= max_uid; i++)
  874.     label_depth[i] = -1;
  875.  
  876.   /* In this loop, for each forward branch we record the stack
  877.      depth of the label it jumps to.  We take advantage of the fact
  878.      that the stack depth at a label reached by a backward branch
  879.      is always, in GCC output, equal to the stack depth of the preceding
  880.      unconditional jump, because it was either a loop statement or
  881.      statement label.  */
  882.  
  883.   for (insn = first; insn; insn = NEXT_INSN (insn))
  884.     {
  885.       rtx pattern = PATTERN (insn);
  886.       switch (GET_CODE (insn))
  887.     {
  888.     case INSN:
  889.       alter_frame_pointer_addresses (pattern, depth);
  890. #ifdef PUSH_ROUNDING
  891.       /* Notice pushes and pops; update DEPTH.  */
  892.       if (GET_CODE (pattern) == SET)
  893.         {
  894.           if (push_operand (SET_DEST (pattern),
  895.                 GET_MODE (SET_DEST (pattern))))
  896.         depth += PUSH_ROUNDING (GET_MODE_SIZE (GET_MODE (SET_DEST (pattern))));
  897.           if (GET_CODE (SET_DEST (pattern)) == REG
  898.           && REGNO (SET_DEST (pattern)) == STACK_POINTER_REGNUM)
  899.         {
  900.           int delta;
  901.           if (GET_CODE (SET_SRC (pattern)) == PLUS
  902.               && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  903.               && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  904.             delta = INTVAL (XEXP (SET_SRC (pattern), 1));
  905.           else if (GET_CODE (SET_SRC (pattern)) == MINUS
  906.                && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  907.                && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  908.             delta = -INTVAL (XEXP (SET_SRC (pattern), 1));
  909.           else abort ();
  910. #ifdef STACK_GROWS_DOWNWARD
  911.           depth -= delta;
  912. #else
  913.           depth += delta;
  914. #endif
  915.         }
  916.         }
  917. #endif
  918.       break;
  919.  
  920.     case JUMP_INSN:
  921.       alter_frame_pointer_addresses (pattern, depth);
  922.       if (GET_CODE (pattern) == ADDR_VEC)
  923.         for (i = 0; i < XVECLEN (pattern, 0); i++)
  924.           label_depth[INSN_UID (XEXP (XVECEXP (pattern, 0, i), 0))] = depth;
  925.       else if (GET_CODE (pattern) == ADDR_DIFF_VEC)
  926.         {
  927.           label_depth[INSN_UID (XEXP (XEXP (pattern, 0), 0))] = depth;
  928.           for (i = 0; i < XVECLEN (pattern, 1); i++)
  929.         label_depth[INSN_UID (XEXP (XVECEXP (pattern, 1, i), 0))] = depth;
  930.         }
  931.       else if (JUMP_LABEL (insn))
  932.         label_depth[INSN_UID (JUMP_LABEL (insn))] = depth;
  933.       else
  934.       break;
  935.  
  936.     case CODE_LABEL:
  937.       if (label_depth [INSN_UID (insn)] >= 0)
  938.         depth = label_depth [INSN_UID (insn)];
  939.       break;
  940.  
  941.     case CALL_INSN:
  942.       alter_frame_pointer_addresses (pattern, depth);
  943.       break;
  944.     }
  945.     }
  946. }
  947.  
  948. /* Walk the rtx X, converting all frame-pointer refs to stack-pointer refs
  949.    on the assumption that the current temporary stack depth is DEPTH.
  950.    (The size of saved registers must be added to DEPTH
  951.    to get the actual offset between the logical frame-pointer and the
  952.    stack pointer.  FIX_FRAME_POINTER_ADDRESS takes care of that.)  */
  953.  
  954. static rtx
  955. alter_frame_pointer_addresses (x, depth)
  956.      register rtx x;
  957.      int depth;
  958. {
  959.   register int i;
  960.   register char *fmt;
  961.   register enum rtx_code code = GET_CODE (x);
  962.  
  963.   switch (code)
  964.     {
  965.     case CONST_INT:
  966.     case CONST:
  967.     case SYMBOL_REF:
  968.     case LABEL_REF:
  969.     case CONST_DOUBLE:
  970.     case CC0:
  971.     case PC:
  972.       return x;
  973.  
  974.     case REG:
  975.       /* Frame ptr can occur outside a PLUS if a stack slot
  976.      can occur with offset 0.  */
  977.       if (x == frame_pointer_rtx)
  978.     {
  979.       FIX_FRAME_POINTER_ADDRESS (x, depth);
  980.     }
  981.       return x;
  982.  
  983.     case MEM:
  984.       {
  985.     rtx addr = XEXP (x, 0);
  986.     FIX_FRAME_POINTER_ADDRESS (addr, depth);
  987.     XEXP (x, 0) = addr;
  988.       }
  989.       break;
  990.  
  991.     case PLUS:
  992.       /* Handle addresses being loaded or pushed, etc.,
  993.      rather than referenced.  */
  994.       FIX_FRAME_POINTER_ADDRESS (x, depth);
  995.       break;
  996.     }
  997.  
  998.   fmt = GET_RTX_FORMAT (code);
  999.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1000.     {
  1001.       if (fmt[i] == 'e')
  1002.     XEXP (x, i) = alter_frame_pointer_addresses (XEXP (x, i), depth);
  1003.       else if (fmt[i] == 'E')
  1004.     {
  1005.       register int j;
  1006.       for (j = XVECLEN (x, i) - 1; j >=0; j--)
  1007.         XVECEXP (x, i, j)
  1008.           = alter_frame_pointer_addresses (XVECEXP (x, i, j), depth);
  1009.     }
  1010.     }
  1011.   return x;
  1012. }
  1013.  
  1014. static void
  1015. alter_reg (i)
  1016.      register int i;
  1017. {
  1018.   /* If the reg got changed to a MEM at rtl-generation time,
  1019.      ignore it.  */
  1020.   if (GET_CODE (regno_reg_rtx[i]) != REG)
  1021.     return;
  1022.  
  1023.   /* Modify the reg-rtx to contain the new hard reg
  1024.      number or else to contain its pseudo reg number.  */
  1025.   REGNO (regno_reg_rtx[i])
  1026.     = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
  1027.  
  1028.   if (reg_renumber[i] < 0 && reg_equiv_init[i])
  1029.     {
  1030.       /* Delete the insn that loads the pseudo register.  */
  1031.       PUT_CODE (reg_equiv_init[i], NOTE);
  1032.       NOTE_LINE_NUMBER (reg_equiv_init[i])
  1033.     = NOTE_INSN_DELETED;
  1034.       NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
  1035.     }
  1036.  
  1037.   /* If we have a pseudo that is needed but has no hard reg or equivalent,
  1038.      allocate a stack slot for it.  */
  1039.  
  1040.   if (reg_renumber[i] < 0
  1041.       && reg_n_refs[i] > 0
  1042.       && reg_equiv_constant[i] == 0
  1043.       && reg_equiv_mem[i] == 0)
  1044.     {
  1045.       register rtx x = assign_stack_local (GET_MODE (regno_reg_rtx[i]),
  1046.                        PSEUDO_REGNO_BYTES (i));
  1047.       register rtx addr = XEXP (x, 0);
  1048.       /* If the stack slot is directly addressable, substitute
  1049.      the MEM we just got directly for the old REG.
  1050.      Otherwise, record the address; we will generate hairy code
  1051.      to compute the address in a register each time it is needed.  */
  1052.       if (memory_address_p (GET_MODE (regno_reg_rtx[i]), addr))
  1053.     reg_equiv_mem[i] = x;
  1054.       else
  1055.     reg_equiv_address[i] = XEXP (x, 0);
  1056.     }
  1057. }
  1058.  
  1059. /* Mark the slots in regs_ever_live for the hard regs
  1060.    used by pseudo-reg number REGNO.  */
  1061.  
  1062. void
  1063. mark_home_live (regno)
  1064.      int regno;
  1065. {
  1066.   register int i, lim;
  1067.   i = reg_renumber[regno];
  1068.   if (i < 0)
  1069.     return;
  1070.   lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
  1071.   while (i < lim)
  1072.     regs_ever_live[i++] = 1;
  1073. }
  1074.  
  1075. /* Kick all pseudos out of hard register REGNO.
  1076.    If GLOBAL is nonzero, try to find someplace else to put them.
  1077.    If DUMPFILE is nonzero, log actions taken on that file.
  1078.  
  1079.    Return nonzero if any pseudos needed to be kicked out.  */
  1080.  
  1081. static int
  1082. spill_hard_reg (regno, global, dumpfile)
  1083.      register int regno;
  1084.      int global;
  1085.      FILE *dumpfile;
  1086. {
  1087.   int something_changed = 0;
  1088.   register int i;
  1089.  
  1090.   /* Spill every pseudo reg that was allocated to this reg
  1091.      or to something that overlaps this reg.  */
  1092.  
  1093.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1094.     if (reg_renumber[i] >= 0
  1095.     && reg_renumber[i] <= regno
  1096.     && (reg_renumber[i] 
  1097.         + HARD_REGNO_NREGS (reg_renumber[i],
  1098.                 PSEUDO_REGNO_MODE (i))
  1099.         > regno))
  1100.       {
  1101. #if 1
  1102.     /* If this register belongs solely to a basic block
  1103.        which needed no spilling, leave it be.  */
  1104.     if (basic_block_needs
  1105.         && reg_basic_block[i] >= 0
  1106.         && basic_block_needs[reg_basic_block[i]] == 0)
  1107.       continue;
  1108. #endif
  1109.  
  1110.     /* Mark it as no longer having a hard register home.  */
  1111.     reg_renumber[i] = -1;
  1112.     /* We will need to scan everything again.  */
  1113.     something_changed = 1;
  1114.     if (global)
  1115.       {
  1116.         retry_global_alloc (i, forbidden_regs);
  1117.         /* Update regs_ever_live for new home (if any).  */
  1118.         mark_home_live (i);
  1119.         /* If something gets spilled to the stack,
  1120.            we must have a frame pointer, so spill the frame pointer.  */
  1121.         if (reg_renumber[i] == -1 && ! frame_pointer_needed)
  1122.           {
  1123.         frame_pointer_needed = 1;
  1124.         forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  1125.         spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  1126.           }
  1127.       }
  1128.     alter_reg (i);
  1129.     if (dumpfile)
  1130.       {
  1131.         if (reg_renumber[i] == -1)
  1132.           fprintf (dumpfile, " Register %d now on stack.\n\n", i);
  1133.         else
  1134.           fprintf (dumpfile, " Register %d now in %d.\n\n",
  1135.                i, reg_renumber[i]);
  1136.       }
  1137.       }
  1138.  
  1139.   return something_changed;
  1140. }
  1141.  
  1142. struct hard_reg_n_uses { int regno; int uses; };
  1143.  
  1144. static int
  1145. hard_reg_use_compare (p1, p2)
  1146.      struct hard_reg_n_uses *p1, *p2;
  1147. {
  1148.   return p1->uses - p2->uses;
  1149. }
  1150.  
  1151. /* Choose the order to consider regs for use as reload registers
  1152.    based on how much trouble would be caused by spilling one.
  1153.    Store them in order of decreasing preference in potential_reload_regs.  */
  1154.  
  1155. static void
  1156. order_regs_for_reload ()
  1157. {
  1158.   register int i;
  1159.   register int o = 0;
  1160.   int large = 0;
  1161.  
  1162.   struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
  1163.  
  1164.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1165.     potential_reload_regs[i] = -1;
  1166.  
  1167.   /* Count number of uses of each hard reg by pseudo regs allocated to it
  1168.      and then order them by decreasing use.  */
  1169.  
  1170.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1171.     {
  1172.       hard_reg_n_uses[i].uses = 0;
  1173.       hard_reg_n_uses[i].regno = i;
  1174.     }
  1175.  
  1176.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1177.     {
  1178.       if (reg_renumber[i] >= 0)
  1179.     hard_reg_n_uses[reg_renumber[i]].uses += reg_n_refs[i];
  1180.       large += reg_n_refs[i];
  1181.     }
  1182.  
  1183.   /* Now fixed registers (which cannot safely be used for reloading)
  1184.      get a very high use count so they will be considered least desirable.
  1185.      Likewise registers used explicitly in the rtl code.  */
  1186.  
  1187.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1188.     if (fixed_regs[i] || regs_explicitly_used[i])
  1189.       hard_reg_n_uses[i].uses = large;
  1190.   hard_reg_n_uses[FRAME_POINTER_REGNUM].uses = large;
  1191.  
  1192.   qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
  1193.      sizeof hard_reg_n_uses[0], hard_reg_use_compare);
  1194.  
  1195.   /* Prefer registers not so far used, for use in temporary loading.
  1196.      Among them, prefer registers not preserved by calls.  */
  1197.  
  1198.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1199.     if (regs_ever_live[i] == 0 && call_used_regs[i]
  1200.     && ! fixed_regs[i])
  1201.       potential_reload_regs[o++] = i;
  1202.  
  1203.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1204.     {
  1205. #ifdef REG_ALLOC_ORDER
  1206.       int regno = reg_alloc_order[i];
  1207. #else
  1208.       int regno = i;
  1209. #endif
  1210.       if (regs_ever_live[regno] == 0 && ! call_used_regs[regno]
  1211.       && regno != FRAME_POINTER_REGNUM)
  1212.     potential_reload_regs[o++] = regno;
  1213.     }
  1214.  
  1215.   /* Now add the regs that are already used,
  1216.      preferring those used less often.  */
  1217.  
  1218.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1219.     if (regs_ever_live[hard_reg_n_uses[i].regno] != 0)
  1220.       potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
  1221.  
  1222. #if 0
  1223.   /* For regs that are used, don't prefer those not preserved by calls
  1224.      because those are likely to contain high priority things
  1225.      that are live for short periods of time.  */
  1226.  
  1227.   for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  1228.     if (regs_ever_live[i] != 0 && ! call_used_regs[i])
  1229.       potential_reload_regs[o++] = i;
  1230. #endif
  1231. }
  1232.  
  1233. /* Reload pseudo-registers into hard regs around each insn as needed.
  1234.    Additional register load insns are output before the insn that needs it
  1235.    and perhaps store insns after insns that modify the reloaded pseudo reg.
  1236.  
  1237.    reg_last_reload_reg and reg_reloaded_contents keep track of
  1238.    which pseudo-registers are already available in reload registers.
  1239.    We update these for the reloads that we perform,
  1240.    as the insns are scanned.  */
  1241.  
  1242. static void
  1243. reload_as_needed (first, live_known)
  1244.      rtx first;
  1245.      int live_known;
  1246. {
  1247.   register rtx insn;
  1248.   register int i;
  1249.  
  1250.   /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
  1251.      Set spill_indirect_ok if so.  */
  1252.   register rtx tem
  1253.     = gen_rtx (MEM, SImode,
  1254.            gen_rtx (PLUS, Pmode,
  1255.             gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
  1256.             gen_rtx (CONST_INT, VOIDmode, 4)));
  1257.  
  1258.   spill_indirect_ok = memory_address_p (QImode, tem);
  1259.  
  1260.   bzero (spill_reg_rtx, sizeof spill_reg_rtx);
  1261.   reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
  1262.   bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
  1263.   reg_has_output_reload = (char *) alloca (max_regno);
  1264.   for (i = 0; i < n_spills; i++)
  1265.     reg_reloaded_contents[i] = -1;
  1266.  
  1267.   for (insn = first; insn;)
  1268.     {
  1269.       register rtx next = NEXT_INSN (insn);
  1270.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  1271.       || GET_CODE (insn) == CALL_INSN)
  1272.     {
  1273.       if (GET_MODE (insn) == VOIDmode)
  1274.         n_reloads = 0;
  1275.       /* First find the pseudo regs that must be reloaded for this insn.
  1276.          This info is returned in the tables reload_... (see reload.h).
  1277.          Also modify the body of INSN by substituting RELOAD
  1278.          rtx's for those pseudo regs.  */
  1279.       else
  1280.         find_reloads (insn, 1, spill_indirect_ok, live_known, spill_reg_order);
  1281.  
  1282.       if (n_reloads > 0)
  1283.         {
  1284.           /* Now compute which reload regs to reload them into.  Perhaps
  1285.          reusing reload regs from previous insns, or else output
  1286.          load insns to reload them.  Maybe output store insns too.
  1287.          Record the choices of reload reg in reload_reg_rtx.  */
  1288.           choose_reload_targets (insn, n_spills);
  1289.           /* Substitute the chosen reload regs from reload_reg_rtx
  1290.          into the insn's body (or perhaps into the bodies of other
  1291.          load and store insn that we just made for reloading
  1292.          and that we moved the structure into).  */
  1293.           subst_reloads ();
  1294.         }
  1295.       /* Any previously reloaded spilled pseudo reg, stored in this insn,
  1296.          is no longer validly lying around to save a future reload.
  1297.          Note that this does not detect pseudos that were reloaded
  1298.          for this insn in order to be stored in
  1299.          (obeying register constraints).  That is correct; such reload
  1300.          registers ARE still valid.  */
  1301.       forget_old_reloads (PATTERN (insn));
  1302.     }
  1303.       /* A reload reg's contents are unknown after a label.  */
  1304.       if (GET_CODE (insn) == CODE_LABEL)
  1305.     for (i = 0; i < n_spills; i++)
  1306.       reg_reloaded_contents[i] = -1;
  1307.  
  1308.       /* Don't assume a reload reg is still good after a call insn
  1309.      if it is a call-used reg.  */
  1310.       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
  1311.     for (i = 0; i < n_spills; i++)
  1312.       if (call_used_regs[spill_regs[i]])
  1313.         reg_reloaded_contents[i] = -1;
  1314.  
  1315.       /* In case registers overlap, allow certain insns to invalidate
  1316.      particular hard registers.  */
  1317.  
  1318. #ifdef INSN_CLOBBERS_REGNO_P
  1319.       for (i = 0 ; i < n_spills ; i++)
  1320.     if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
  1321.       reg_reloaded_contents[i] = -1;
  1322. #endif
  1323.  
  1324.       insn = next;
  1325.     }
  1326. }
  1327.  
  1328. /* If we see a pseudo-reg being stored into,
  1329.    don't try to reuse an old reload reg
  1330.    which previously contained a copy of it.  */
  1331.  
  1332. static void
  1333. forget_old_reloads (x)
  1334.      rtx x;
  1335. {
  1336.   if (GET_CODE (x) == SET && GET_CODE (SET_DEST (x)) == REG)
  1337.     {
  1338.       register int regno = REGNO (SET_DEST (x));
  1339.       int nr;
  1340.  
  1341.       if (regno >= FIRST_PSEUDO_REGISTER)
  1342.     nr = 1;
  1343.       /* Don't do this for spill regs.  If a spill reg got in there
  1344.      it is due to an output reload, which is still valid.  */
  1345.       else if (spill_reg_order[regno] >= 0)
  1346.     nr = 0;
  1347.       else
  1348.     {
  1349.       int i;
  1350.       nr = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  1351.       /* Storing into a spilled-reg invalidates its contents.
  1352.          This can happen if a block-local pseudo is allocated to that reg
  1353.          and it wasn't spilled because this block's total need is 0.
  1354.          Then some insn might have an optional reload and use this reg.  */
  1355.       for (i = 0; i < nr; i++)
  1356.         if (spill_reg_order[regno + i] >= 0
  1357.         && reg_has_output_reload[regno + i] == 0)
  1358.           reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
  1359.     }
  1360.       
  1361.       while (nr-- > 0) reg_last_reload_reg[regno + nr] = 0;
  1362.     }
  1363.   else if (GET_CODE (x) == PARALLEL)
  1364.     {
  1365.       register int i;
  1366.       for (i = 0; i < XVECLEN (x, 0); i++)
  1367.     {
  1368.       register rtx y = XVECEXP (x, 0, i);
  1369.       if (GET_CODE (y) == SET && GET_CODE (SET_DEST (y)) == REG)
  1370.         forget_old_reloads (y);
  1371.     }
  1372.     }
  1373. }
  1374.  
  1375. /* Comparison function for qsort to decide which of two reloads
  1376.    should be handled first.  *P1 and *P2 are the reload numbers.  */
  1377.  
  1378. static int
  1379. reload_reg_class_lower (p1, p2)
  1380.      short *p1, *p2;
  1381. {
  1382.   register int r1 = *p1, r2 = *p2;
  1383.   register int t;
  1384.   register enum machine_mode mode1, mode2;
  1385.   
  1386.   /* Consider required reloads before optional ones.  */
  1387.   t = reload_optional[r1] - reload_optional[r2];
  1388.   if (t != 0)
  1389.     return t;
  1390.   /* Consider all multi-reg groups first.
  1391.      This is safe because `reload' fills all group-need before
  1392.      filling all non-group need.  */
  1393.   mode1 = (reload_inmode[r1] == VOIDmode ? reload_outmode[r1] : reload_inmode[r1]);
  1394.   mode2 = (reload_inmode[r2] == VOIDmode ? reload_outmode[r2] : reload_inmode[r2]);
  1395.   t = (CLASS_MAX_NREGS (reload_reg_class[r2], mode2)
  1396.        - CLASS_MAX_NREGS (reload_reg_class[r1], mode1));
  1397.   if (t != 0)
  1398.     return t;
  1399.   /* Consider reloads in order of increasing reg-class number.  */
  1400.   t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
  1401.   return t;
  1402. }
  1403.  
  1404. /* Assign hard reg targets for the pseudo-registers we must reload
  1405.    into hard regs for this insn.
  1406.    Also output the instructions to copy them in and out of the hard regs.
  1407.  
  1408.    For machines with register classes, we are responsible for
  1409.    finding a reload reg in the proper class.  */
  1410.  
  1411. static void
  1412. choose_reload_targets (insn)
  1413.      rtx insn;
  1414. {
  1415.   register int j;
  1416.   char reload_reg_in_use[FIRST_PSEUDO_REGISTER];
  1417.   short reload_order[FIRST_PSEUDO_REGISTER];
  1418.   char reload_inherited[FIRST_PSEUDO_REGISTER];
  1419.   int have_groups = 0;
  1420.  
  1421.   /* For each reload, the index in spill_regs of the spill register used,
  1422.      or -1 if we did not need one of the spill registers for this reload.  */
  1423.   int reload_spill_index[FIRST_PSEUDO_REGISTER];
  1424.  
  1425.   bzero (reload_inherited, FIRST_PSEUDO_REGISTER);
  1426.   bzero (reload_reg_in_use, FIRST_PSEUDO_REGISTER);
  1427.   bzero (reg_has_output_reload, max_regno);
  1428.  
  1429.   /* In order to be certain of getting the registers we need,
  1430.      we must sort the reloads into order of increasing register class.
  1431.      Then our grabbing of reload registers will parallel the process
  1432.      that provided the reload registers.  */
  1433.  
  1434.   /* Also note whether any of the reloads wants a consecutive group of regs.
  1435.      When that happens, we must when processing the non-group reloads
  1436.      avoid (when possible) using a reload reg that would break up a group.  */
  1437.  
  1438.   /* This used to look for an existing reloaded home for all
  1439.      of the reloads, and only then perform any new reloads.
  1440.      But that could lose if the reloads were done out of reg-class order
  1441.      because a later reload with a looser constraint might have an old
  1442.      home in a register needed by an earlier reload with a tighter constraint.
  1443.      It would be possible with even hairier code to detect such cases
  1444.      and handle them, but it doesn't seem worth while yet.  */
  1445.  
  1446.   for (j = 0; j < n_reloads; j++)
  1447.     {
  1448.       enum machine_mode mode;
  1449.       reload_order[j] = j;
  1450.       reload_spill_index[j] = -1;
  1451.       mode = (reload_inmode[j] == VOIDmode ? reload_outmode[j] : reload_inmode[j]);
  1452.       if (CLASS_MAX_NREGS (reload_reg_class[j], mode) > 1)
  1453.     have_groups = 1;
  1454.     }
  1455.  
  1456.   if (n_reloads > 1)
  1457.     qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
  1458.  
  1459.   for (j = 0; j < n_reloads; j++)
  1460.     {
  1461.       register int r = reload_order[j];
  1462.       register int i;
  1463.       register rtx new;
  1464.       enum machine_mode reload_mode = reload_inmode[r];
  1465.  
  1466.       if (GET_MODE_SIZE (reload_outmode[r]) > GET_MODE_SIZE (reload_mode))
  1467.     reload_mode = reload_outmode[r];
  1468.       if (reload_strict_low[r])
  1469.     reload_mode = GET_MODE (SUBREG_REG (reload_out[r]));
  1470.  
  1471.       /* Ignore reloads that got marked inoperative.  */
  1472.       if (reload_out[r] == 0 && reload_in[r] == 0)
  1473.     continue;
  1474.  
  1475.       /* No need to find a reload-register if find_reloads chose one.  */
  1476.  
  1477.       if (reload_reg_rtx[r] != 0)
  1478.     {
  1479. #if 0
  1480.       /* But do see if the chosen reload-reg already contains
  1481.          a copy of the desired value.  */
  1482.       if (reload_in[r] != 0)
  1483.         {
  1484.           register rtx equiv
  1485.         = find_equiv_reg (reload_in[r], insn, 0,
  1486.                   REGNO (reload_reg_rtx[r]), 0, 0,
  1487.                   reload_mode);
  1488.           if (equiv != 0)
  1489.         reload_inherited[r] = 1;
  1490.         }
  1491. #endif
  1492.       continue;
  1493.     }
  1494.  
  1495.       /* First see if this pseudo is already available as reloaded
  1496.      for a previous insn.
  1497.      This feature is disabled for multi-register groups
  1498.      because we haven't yet any way to tell whether the entire
  1499.      value is properly preserved.
  1500.      It is also disabled when there are other reloads for mult-register
  1501.      groups, lest the inherited reload reg break up a needed group.  */
  1502.  
  1503.       {
  1504.     register int regno = -1;
  1505.  
  1506.     if (reload_in[r] == 0)
  1507.       ;
  1508.     else if (GET_CODE (reload_in[r]) == REG)
  1509.       regno = REGNO (reload_in[r]);
  1510. #if 0
  1511.     /* This won't work, since REGNO can be a pseudo reg number.
  1512.        Also, it takes much more hair to keep track of all the things
  1513.        that can invalidate an inherited reload of part of a pseudoreg.  */
  1514.     else if (GET_CODE (reload_in[r]) == SUBREG
  1515.          && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
  1516.       regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
  1517. #endif
  1518.  
  1519.     if (regno >= 0
  1520.         && reg_last_reload_reg[regno] != 0
  1521.         && ! have_groups)
  1522.       {
  1523.         i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
  1524.  
  1525.         if (reg_reloaded_contents[i] == regno
  1526.         && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
  1527.                       spill_regs[i])
  1528.         && ! reload_reg_in_use[spill_regs[i]])
  1529.           {
  1530.         /* Mark the reload register as in use for this insn.  */
  1531.         reload_reg_rtx[r] = reg_last_reload_reg[regno];
  1532.         reload_reg_in_use[spill_regs[i]] = 1;
  1533.         reload_inherited[r] = 1;
  1534.         reload_spill_index[r] = i;
  1535.           }
  1536.       }
  1537.       }
  1538.  
  1539.       /* If this is not a pseudo, here's a different way to see
  1540.      if it is already lying around.  */
  1541.       if (reload_in[r] != 0
  1542.       && reload_out[r] == 0
  1543.       && (CONSTANT_P (reload_in[r])
  1544.           || GET_CODE (reload_in[r]) == PLUS
  1545.           || GET_CODE (reload_in[r]) == MEM)
  1546.       && ! have_groups)
  1547.     {
  1548.       register rtx equiv
  1549.         = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
  1550.                   -1, (short *)1, 0, reload_mode);
  1551.       /* If we found an equivalent reg, say no code need be generated
  1552.          to load it, and use it as our reload reg.  */
  1553.       if (equiv != 0
  1554.           && REGNO (equiv) != FRAME_POINTER_REGNUM)
  1555.         {
  1556.           reload_reg_rtx[r] = equiv;
  1557.           reload_inherited[r] = 1;
  1558.           /* If it is a spill reg,
  1559.          mark the spill reg as in use for this insn.  */
  1560.           i = spill_reg_order[REGNO (equiv)];
  1561.           if (i >= 0)
  1562.         {
  1563.           int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1564.           while (nr > 0)
  1565.             reload_reg_in_use[REGNO (equiv) + --nr] = 1;
  1566.         }
  1567.         }
  1568.     }
  1569.  
  1570.       /* If it isn't lying around, and isn't optional,
  1571.      find a place to reload it into.  */
  1572.       if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
  1573.     continue;
  1574.  
  1575.       /* Value not lying around; find a register to reload it into.
  1576.      Here I is not a regno, it is an index into spill_regs.  */
  1577.       i = n_spills;
  1578.  
  1579. #if 0
  1580.       /* The following is no longer needed now that all multi-register
  1581.      (group) reloads are processed before all solitary register reloads
  1582.      (due to changes in `reg_class_lower_p' and `reload'.  */
  1583.       /* The following also fails to test HARD_REGNO_MODE_OK appropriately,
  1584.      which was hard to fix because we don't know the mode that the
  1585.      group might have that would want this register.  */
  1586.  
  1587.       /* If we want just one reg, and other reloads want groups,
  1588.      first try to find a reg that can't be part of a group.  */
  1589.       if (have_groups
  1590.       && CLASS_MAX_NREGS (reload_reg_class[r], reload_mode) == 1)
  1591.     for (i = 0; i < n_spills; i++)
  1592.       {
  1593.         int regno = spill_regs[i];
  1594.         int class = (int) reload_reg_class[r];
  1595.         if (reload_reg_in_use[regno] == 0
  1596.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1597.                       regno)
  1598.         && !(regno + 1 < FIRST_PSEUDO_REGISTER
  1599.              && spill_reg_order[regno + 1] >= 0
  1600.              && reload_reg_in_use[regno + 1] == 0
  1601.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1602.                        regno + 1))
  1603.         && !(regno > 0
  1604.              && spill_reg_order[regno - 1] >= 0
  1605.              && reload_reg_in_use[regno - 1] == 0
  1606.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1607.                        regno - 1)))
  1608.           break;
  1609.       }
  1610.  
  1611.       /* If that didn't work, try to find a register that has only one
  1612.      neighbor that could make a group with it.  That way, if the
  1613.      available registers are three consecutive ones, we avoid taking
  1614.      the middle one (which would leave us with no possible groups).  */
  1615.  
  1616.       if (have_groups
  1617.       && CLASS_MAX_NREGS (reload_reg_class[r], reload_mode) == 1
  1618.       && i == n_spills)
  1619.     for (i = 0; i < n_spills; i++)
  1620.       {
  1621.         int regno = spill_regs[i];
  1622.         int class = (int) reload_reg_class[r];
  1623.         if (reload_reg_in_use[regno] == 0
  1624.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1625.                       regno)
  1626.         && (!(regno + 1 < FIRST_PSEUDO_REGISTER
  1627.               && spill_reg_order[regno + 1] >= 0
  1628.               && reload_reg_in_use[regno + 1] == 0
  1629.               && TEST_HARD_REG_BIT (reg_class_contents[class],
  1630.                         regno + 1))
  1631.             || !(regno > 0
  1632.              && spill_reg_order[regno - 1] >= 0
  1633.              && reload_reg_in_use[regno - 1] == 0
  1634.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1635.                            regno - 1))))
  1636.           break;
  1637.       }
  1638. #endif
  1639.  
  1640.       /* Now, if we want a single register and haven't yet found one,
  1641.      take any reg in the right class and not in use.
  1642.      If we want a consecutive group, here is where we look for it.  */
  1643.       if (i == n_spills)
  1644.     for (i = 0; i < n_spills; i++)
  1645.       {
  1646.         int class = (int) reload_reg_class[r];
  1647.         if (reload_reg_in_use[spill_regs[i]] == 0
  1648.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1649.                       spill_regs[i]))
  1650.           {
  1651.         int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1652.         /* If we need only one reg, we have already won.  */
  1653.         if (nr == 1)
  1654.           break;
  1655.         /* Otherwise check that as many consecutive regs as we need
  1656.            are available here.  */
  1657.         if (HARD_REGNO_MODE_OK (spill_regs[i], reload_mode))
  1658.           while (nr > 1)
  1659.             {
  1660.               if (!(TEST_HARD_REG_BIT (reg_class_contents[class],
  1661.                            spill_regs[i] + nr - 1)
  1662.                 && spill_reg_order[spill_regs[i] + nr - 1] >= 0
  1663.                 && reload_reg_in_use[spill_regs[i] + nr - 1] == 0))
  1664.             break;
  1665.               nr--;
  1666.             }
  1667.         if (nr == 1)
  1668.           break;
  1669.           }
  1670.       }
  1671.  
  1672.       /* We should have found a spill register by now.  */
  1673.       if (i == n_spills)
  1674.     abort ();
  1675.  
  1676.       /* Mark as in use for this insn the reload regs we use for this.  */
  1677.       {
  1678.     int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1679.     while (nr > 0)
  1680.       reload_reg_in_use[spill_regs[i] + --nr] = 1;
  1681.       }
  1682.  
  1683.       new = spill_reg_rtx[i];
  1684.  
  1685.       if (new == 0 || GET_MODE (new) != reload_mode)
  1686.     spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode, spill_regs[i]);
  1687.  
  1688.       reload_reg_rtx[r] = new;
  1689.       reload_spill_index[r] = i;
  1690.       reg_reloaded_contents[i] = -1;
  1691.  
  1692.       /* Detect when the reload reg can't hold the reload mode.  */
  1693.       if (! HARD_REGNO_MODE_OK (REGNO (reload_reg_rtx[r]), reload_mode))
  1694.     {
  1695.       if (! asm_noperands (PATTERN (insn)))
  1696.         /* It's the compiler's fault.  */
  1697.         abort ();
  1698.       /* It's the user's fault; the operand's mode and constraint
  1699.          don't match.  Disable this reload so we don't crash in final.
  1700.          Maybe we should print an error message too??  */
  1701.       reload_in[r] = 0;
  1702.       reload_out[r] = 0;
  1703.       reload_reg_rtx[r] = 0;
  1704.       reload_optional[r] = 1;
  1705.     }
  1706.     }
  1707.  
  1708.   /* For all the spill regs newly reloaded in this instruction,
  1709.      record what they were reloaded from, so subsequent instructions
  1710.      can inherit the reloads.  */
  1711.  
  1712.   for (j = 0; j < n_reloads; j++)
  1713.     {
  1714.       register int r = reload_order[j];
  1715.       register int i = reload_spill_index[r];
  1716.  
  1717.       /* I is nonneg if this reload used one of the spill regs.
  1718.      If reload_reg_rtx[r] is 0, this is an optional reload
  1719.      that we opted to ignore.  */
  1720.       if (i >= 0 && reload_reg_rtx[r] != 0)
  1721.     {
  1722.       /* Maybe the spill reg contains a copy of reload_out.  */
  1723.       if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
  1724.         {
  1725.           register int nregno = REGNO (reload_out[r]);
  1726.           reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1727.           reg_reloaded_contents[i] = nregno;
  1728.           reg_has_output_reload[nregno] = 1;
  1729.         }
  1730.       /* Maybe the spill reg contains a copy of reload_in.  */
  1731.       else if (reload_out[r] == 0 && GET_CODE (reload_in[r]) == REG)
  1732.         {
  1733.           register int nregno = REGNO (reload_in[r]);
  1734.           /* If there are two separate reloads (one in and one out)
  1735.          for the same (hard or pseudo) reg, set reg_last_reload_reg
  1736.          based on the output reload.  */
  1737.           if (!reg_has_output_reload[nregno])
  1738.         {
  1739.           reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1740.           reg_reloaded_contents[i] = nregno;
  1741.         }
  1742.         }
  1743.       /* Otherwise, the spill reg doesn't contain a copy of any reg.
  1744.          Clear out its records, lest it be taken for a copy
  1745.          of reload_in when that is no longer true.  */
  1746.       else
  1747.         reg_reloaded_contents[i] = -1;
  1748.     }
  1749.     }
  1750.  
  1751.   /* Now output the instructions to copy the data into and out of the
  1752.      reload registers.  Do these in the order that the reloads were reported,
  1753.      since reloads of base and index registers precede reloads of operands
  1754.      and the operands may need the base and index registers reloaded.  */
  1755.  
  1756.   for (j = 0; j < n_reloads; j++)
  1757.     {
  1758.       register rtx old;
  1759.       rtx store_insn;
  1760.  
  1761.       old = reload_in[j];
  1762.       if (old != 0 && ! reload_inherited[j]
  1763.       && reload_reg_rtx[j] != old
  1764.       && reload_reg_rtx[j] != 0)
  1765.     {
  1766.       register rtx reloadreg = reload_reg_rtx[j];
  1767.       rtx oldequiv = 0;
  1768.       enum machine_mode mode;
  1769.  
  1770.       /* Strip off of OLD any size-increasing SUBREGs such as
  1771.          (SUBREG:SI foo:QI 0).  */
  1772.  
  1773.       while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  1774.          && (GET_MODE_SIZE (GET_MODE (old))
  1775.              > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  1776.         old = SUBREG_REG (old);
  1777.  
  1778.       /* Determine the mode to reload in.
  1779.          This is very tricky because we have three to choose from.
  1780.          There is the mode the insn operand wants (reload_inmode[J]).
  1781.          There is the mode of the reload register RELOADREG.
  1782.          There is the intrinsic mode of the operand, revealed now
  1783.          in OLD because we have stripped SUBREGs.
  1784.          It turns out that RELOADREG's mode is irrelevant:
  1785.          we can change that arbitrarily.
  1786.  
  1787.          Neither of the other two is always right.  For example, consider
  1788.          (SUBREG:SI foo:QI)) appearing as an operand that must be SImode;
  1789.          then suppose foo is in memory.  This must be loaded in QImode
  1790.          because we cannot fetch a byte as a word.  In this case OLD's
  1791.          mode is correct.
  1792.  
  1793.          Then consider a one-word union which has SImode and one of its
  1794.          members is a float, being fetched as (SUBREG:SF union:SI).
  1795.          We must fetch that as SFmode because we could be loading into
  1796.          a float-only register.  In this case OLD's mode is also correct.
  1797.  
  1798.          Consider an immediate integer: it has VOIDmode.  Here we need
  1799.          to get a mode from something else.
  1800.  
  1801.          In some cases, there is a fourth mode, the operand's
  1802.          containing mode.  If the insn specifies a containing mode for
  1803.          this operand, it overrides all others.
  1804.  
  1805.          I am not sure whether the algorithm here is always right,
  1806.          but it does the right things in those cases.  */
  1807.  
  1808.       mode = GET_MODE (old);
  1809.       if (mode == VOIDmode)
  1810.         mode = reload_inmode[j];
  1811.       if (reload_strict_low[j])
  1812.         mode = GET_MODE (SUBREG_REG (reload_in[j]));
  1813.  
  1814.       /* If reloading from memory, see if there is a register
  1815.          that already holds the same value.  If so, reload from there.
  1816.          We can pass 0 as the reload_reg_p argument because
  1817.          any other reload has either already been emitted,
  1818.          in which case find_equiv_reg will see the reload-insn,
  1819.          or has yet to be emitted, in which case it doesn't matter
  1820.          because we will use this equiv reg right away.  */
  1821.  
  1822.       if (GET_CODE (old) == MEM
  1823.           || (GET_CODE (old) == REG
  1824.           && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1825.           && reg_renumber[REGNO (old)] < 0))
  1826.         oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
  1827.                        -1, 0, 0, mode);
  1828.  
  1829.       if (oldequiv == 0)
  1830.         oldequiv = old;
  1831.  
  1832.       /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
  1833.          then load RELOADREG from OLDEQUIV.  */
  1834.  
  1835.       if (GET_MODE (reloadreg) != mode)
  1836.         reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  1837.       if (GET_MODE (oldequiv) != VOIDmode
  1838.           && mode != GET_MODE (oldequiv))
  1839.         oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
  1840.  
  1841.       /* If we are reloading a pseudo-register that was set by the previous
  1842.          insn, see if we can get rid of that pseudo-register entirely
  1843.          by redirecting the previous insn into our reload register.  */
  1844.  
  1845.       if (optimize && GET_CODE (old) == REG
  1846.           && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1847.           && dead_or_set_p (insn, old))
  1848.         {
  1849.           rtx temp = PREV_INSN (insn);
  1850.           while (temp && GET_CODE (temp) == NOTE)
  1851.         temp = PREV_INSN (temp);
  1852.           if (temp
  1853.           && GET_CODE (temp) == INSN
  1854.           && GET_CODE (PATTERN (temp)) == SET
  1855.           && SET_DEST (PATTERN (temp)) == old
  1856.           /* Don't risk splitting a matching pair of operands.  */
  1857.           && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
  1858.         {
  1859.           /* Store into the reload register instead of the pseudo.  */
  1860.           SET_DEST (PATTERN (temp)) = reloadreg;
  1861.           /* If these are the only uses of the pseudo reg,
  1862.              pretend for GDB it lives in the reload reg we used.  */
  1863.           if (reg_n_deaths[REGNO (old)] == 1
  1864.               && reg_n_sets[REGNO (old)] == 1)
  1865.             {
  1866.               reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  1867.               alter_reg (REGNO (old));
  1868.             }
  1869.         }
  1870.           else
  1871.         /* We can't do that, so output an insn to load RELOADREG.  */
  1872.         emit_insn_before (gen_move_insn (reloadreg, oldequiv), insn);
  1873.         }
  1874.       else
  1875.         /* We can't do that, so output an insn to load RELOADREG.  */
  1876.         emit_insn_before (gen_move_insn (reloadreg, oldequiv), insn);
  1877.  
  1878. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  1879.       if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
  1880.         {
  1881. #if 0
  1882.           /* An input reload means the original reg is no longer
  1883.          used, therefore no longer dies here.  */
  1884.           /* Deleted because it interferes with deletion (below)
  1885.          of the insn that stored the output reload.
  1886.          Also, who cares about OLDEQUIV's death notes?  */
  1887.  
  1888.           if (REG_P (oldequiv)
  1889.           && regno_dead_p (REGNO (oldequiv), insn))
  1890.         remove_death (REGNO (oldequiv), insn);
  1891. #endif
  1892.  
  1893.           /* Add a death note to this insn, for an input reload.  */
  1894.  
  1895.           if (! dead_or_set_p (insn, reloadreg))
  1896.         REG_NOTES (insn)
  1897.           = gen_rtx (EXPR_LIST, REG_DEAD,
  1898.                  reloadreg, REG_NOTES (insn));
  1899.         }
  1900. #endif
  1901.  
  1902.       /* If this reload wants reload_in[j] incremented by a constant,
  1903.          output code to get this done before the insn reloaded for.  */
  1904.  
  1905.       if (reload_inc[j] != 0)
  1906.         {
  1907.           /* If reload_in[j] is a register, assume we can
  1908.          output an insn to increment it directly.  */
  1909.           if (GET_CODE (old) == REG &&
  1910.           (REGNO (old) < FIRST_PSEUDO_REGISTER
  1911.            || reg_renumber[REGNO (old)] >= 0))
  1912.         emit_insn_before (gen_add2_insn (old,
  1913.                          gen_rtx (CONST_INT, VOIDmode,
  1914.                               reload_inc[j])),
  1915.                   insn);
  1916.           else
  1917.         /* Else we must not assume we can increment reload_in[j]
  1918.            (even though on many target machines we can);
  1919.            increment the copy in the reload register,
  1920.            save that back, then decrement the reload register
  1921.            so it has its original contents.  */
  1922.         {
  1923.           rtx oldreal = old;
  1924.           /* OLDREAL is OLDEQUIV encapsulated in that mode,
  1925.              in case we need to write back to it.
  1926.              OLDEQUIV is good only for reading.  */
  1927.           if (GET_MODE (old) != VOIDmode
  1928.               && mode != GET_MODE (old))
  1929.             oldreal = gen_rtx (SUBREG, mode, oldreal, 0);
  1930.  
  1931.           emit_insn_before (gen_add2_insn (reloadreg,
  1932.                            gen_rtx (CONST_INT, VOIDmode,
  1933.                                 reload_inc[j])),
  1934.                     insn);
  1935.           emit_insn_before (gen_move_insn (oldreal, reloadreg), insn);
  1936.           emit_insn_before (gen_sub2_insn (reloadreg,
  1937.                            gen_rtx (CONST_INT, VOIDmode,
  1938.                                 reload_inc[j])),
  1939.                     insn);
  1940.         }
  1941.         }
  1942.     }
  1943.  
  1944. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  1945.       /* For some registers it is important to keep the REG_DEATH
  1946.      notes accurate for the final pass.
  1947.      If we are inheriting an old output-reload out of such a reg,
  1948.      the reg no longer dies there, so remove the death note.  */
  1949.  
  1950.       if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
  1951.       && reload_inherited[j] && reload_spill_index[j] >= 0
  1952.       && GET_CODE (reload_in[j]) == REG
  1953.       && spill_reg_store[reload_spill_index[j]] != 0
  1954.       && regno_dead_p (REGNO (reload_reg_rtx[j]),
  1955.                spill_reg_store[reload_spill_index[j]]))
  1956.     {
  1957.       remove_death (REGNO (reload_reg_rtx[j]),
  1958.             spill_reg_store[reload_spill_index[j]]);
  1959.     }
  1960. #endif
  1961.  
  1962.       /* If we are reloading a register that was recently stored in with an
  1963.      output-reload, see if we can prove there was
  1964.      actually no need to store the old value in it.  */
  1965.  
  1966.       if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
  1967.       && GET_CODE (reload_in[j]) == REG
  1968.       && spill_reg_store[reload_spill_index[j]] != 0
  1969.       && dead_or_set_p (insn, reload_in[j]))
  1970.     {
  1971.       register rtx i1;
  1972.       /* If the pseudo-reg we are reloading is no longer referenced
  1973.          anywhere between the store into it and here,
  1974.          and no jumps or labels intervene, then the value can get
  1975.          here through the reload reg alone.  */
  1976.       for (i1 = NEXT_INSN (spill_reg_store[reload_spill_index[j]]);
  1977.            i1 != insn; i1 = NEXT_INSN (i1))
  1978.         {
  1979.           if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
  1980.         break;
  1981.           if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
  1982.           && reg_mentioned_p (reload_in[j], PATTERN (i1)))
  1983.         break;
  1984.         }
  1985.       if (i1 == insn)
  1986.         {
  1987.           /* If this insn will store in the pseudo again,
  1988.          the previous store can be removed.  */
  1989.           if (reload_out[j] == reload_in[j])
  1990.         delete_insn (spill_reg_store[reload_spill_index[j]]);
  1991.  
  1992.           /* See if the pseudo reg has been completely replaced
  1993.          with reload regs.  If so, delete the store insn
  1994.          and forget we had a stack slot for the pseudo.  */
  1995.           else if (reg_n_deaths[REGNO (reload_in[j])] == 1
  1996.                && reg_basic_block[REGNO (reload_in[j])] >= 0
  1997.                && find_regno_note (insn, REG_DEAD, REGNO (reload_in[j])))
  1998.         {
  1999.           rtx i2;
  2000.           /* We know that it was used only between here
  2001.              and the beginning of the current basic block.
  2002.              Search that range; see if any ref remains.  */
  2003.           for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
  2004.             {
  2005. /*              if (i2 == spill_reg_store[reload_spill_index[j]]) */
  2006.               /* Uses which just store in the pseudo don't count,
  2007.              since if they are the only uses, they are dead.  */
  2008.               if (GET_CODE (i2) == INSN
  2009.               && GET_CODE (PATTERN (i2)) == SET
  2010.               && SET_DEST (PATTERN (i2)) == reload_in[j])
  2011.             continue;
  2012.               if (GET_CODE (i2) == CODE_LABEL
  2013.               || GET_CODE (i2) == JUMP_INSN)
  2014.             break;
  2015.               if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
  2016.               && reg_mentioned_p (reload_in[j], PATTERN (i2)))
  2017.             goto still_used;
  2018.             }
  2019.           /* Delete the now-dead stores into this pseudo.  */
  2020. /*          delete_insn (spill_reg_store[reload_spill_index[j]]); */
  2021.           for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
  2022.             {
  2023.               /* Uses which just store in the pseudo don't count,
  2024.              since if they are the only uses, they are dead.  */
  2025.               if (GET_CODE (i2) == INSN
  2026.               && GET_CODE (PATTERN (i2)) == SET
  2027.               && SET_DEST (PATTERN (i2)) == reload_in[j])
  2028.             delete_insn (i2);
  2029.               if (GET_CODE (i2) == CODE_LABEL
  2030.               || GET_CODE (i2) == JUMP_INSN)
  2031.             break;
  2032.             }
  2033.           /* For the debugging info,
  2034.              say the pseudo lives in this reload reg.  */
  2035.           reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  2036.           alter_reg (REGNO (old));
  2037.         still_used: ;
  2038.         }
  2039.         }
  2040.     }
  2041.  
  2042.       /* Input-reloading is done.  Now do output-reloading,
  2043.      storing the value from the reload-register after the main insn
  2044.      if reload_out[j] is nonzero.  */
  2045.       old = reload_out[j];
  2046.       if (old != 0
  2047.       && reload_reg_rtx[j] != old
  2048.       && reload_reg_rtx[j] != 0)
  2049.     {
  2050.       register rtx reloadreg = reload_reg_rtx[j];
  2051.       enum machine_mode mode;
  2052.  
  2053.       /* Strip off of OLD any size-increasing SUBREGs such as
  2054.          (SUBREG:SI foo:QI 0).  */
  2055.  
  2056.       while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  2057.          && (GET_MODE_SIZE (GET_MODE (old))
  2058.              > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  2059.         old = SUBREG_REG (old);
  2060.  
  2061.       /* Determine the mode to reload in.
  2062.          See comments above (for input reloading).  */
  2063.  
  2064.       mode = GET_MODE (old);
  2065.       if (mode == VOIDmode)
  2066.         abort ();        /* Should never happen for an output.  */
  2067. #if 0
  2068.         mode = reload_inmode[j];
  2069. #endif
  2070.       if (reload_strict_low[j])
  2071.         mode = GET_MODE (SUBREG_REG (reload_out[j]));
  2072.  
  2073.       /* Encapsulate both RELOADREG and OLD into that mode,
  2074.          then load RELOADREG from OLD.  */
  2075.       if (GET_MODE (reloadreg) != mode)
  2076.         reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  2077.       if (GET_MODE (old) != VOIDmode
  2078.           && mode != GET_MODE (old))
  2079.         old = gen_rtx (SUBREG, mode, old, 0);
  2080.       store_insn = emit_insn_after (gen_move_insn (old, reloadreg), insn);
  2081.       /* If final will look at death notes for this reg,
  2082.          put one on each output-reload insn.  */
  2083. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  2084.       if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
  2085.         REG_NOTES (store_insn)
  2086.         = gen_rtx (EXPR_LIST, REG_DEAD,
  2087.                reloadreg, REG_NOTES (store_insn));
  2088. #endif
  2089.     }
  2090.       else store_insn = 0;
  2091.  
  2092.       if (reload_spill_index[j] >= 0)
  2093.     spill_reg_store[reload_spill_index[j]] = store_insn;
  2094.     }
  2095. }
  2096.